package com.hxzy.servlet.TbPanelContent;

import com.hxzy.entity.TbPanelContent;
import com.hxzy.service.TbPanelContentService;
import com.hxzy.service.TbPanelService;
import com.hxzy.service.impl.TbPanelContentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(urlPatterns = "/TbPanelContent/Insert")
public class Insert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/TbPanelContent/insert.jsp").forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");

        TbPanelContent content=new TbPanelContent();


        /**
         * 所属板块id
         */
         Integer panelId=Integer.parseInt(req.getParameter("panelId"));
         content.setPanelId(panelId);

        /**
         * 类型 0关联商品 1其他链接
         */
         Integer type=Integer.parseInt(req.getParameter("type"));
         content.setType(type);

        /**
         * 关联商品id
         */
        Long productId=Long.parseLong(req.getParameter("productId"));
        content.setProductId(productId);

        Integer sortOrder=Integer.parseInt(req.getParameter("sortOrder"));
        content.setSortOrder(sortOrder);

        /**
         * 其他链接
         */
        String fullUrl=req.getParameter("fullUrl");
        content.setFullUrl(fullUrl);

        String picUrl=req.getParameter("picUrl");
        content.setPicUrl(picUrl);

        /**
         * 3d轮播图备用
         */
        String picUrl2=req.getParameter("picUrl2");
        content.setPicUrl(picUrl2);

        /**
         * 3d轮播图备用
         */
        String picUrl3=req.getParameter("picUrl3");
        content.setPicUrl(picUrl3);

        SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-DD");
        try {
            Date created=sdf.parse(req.getParameter("created"));
            content.setCreated(created);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        try {
            Date updated=sdf.parse(req.getParameter("updated"));
            content.setUpdated(updated);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        TbPanelContentService service=new TbPanelContentServiceImpl();
        boolean insert = service.insert(content);

//        PrintWriter out=res.getWriter();
//        out.println( insert? "新增成功": "新增失败");
//        //输出刷新
//        out.flush();
//        out.close();

        String path=req.getContextPath();
        req.setAttribute("url",path+"/TbPanelContent/search");
        //转发
        req.getRequestDispatcher("/WEB-INF/jump.jsp").forward(req,res);
    }
}
